home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Human Interface Toolbox / Concordia / SizeTkl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.8 KB  |  190 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        SizeTkl.c
  3.  
  4.     Contains:    izeTkl.c contains the code to calculate the size of a pull-down menu.  Also,
  5.                 services are provided through CalItemHeight and CalcItemWidth to calculate the
  6.                 height and width of a single menu item.
  7.  
  8.     Written by:     
  9.  
  10.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.                 You may incorporate this Apple sample source code into your program(s) without
  13.                 restriction. This Apple sample source code has been provided "AS IS" and the
  14.                 responsibility for its operation is yours. You are not permitted to redistribute
  15.                 this Apple sample source code as "Apple sample source code" after having made
  16.                 changes. If you're going to re-distribute the source, we require that you make
  17.                 it clear in the source that the code was descended from Apple sample source
  18.                 code, but that you've made changes.
  19.  
  20.     Change History (most recent first):
  21.                 8/10/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */
  25.  
  26. /******************************************************************************\
  27. * Header Files
  28. \******************************************************************************/
  29.  
  30. #include <Memory.h>
  31. #include <Quickdraw.h>
  32. #include <Script.h>
  33. #include <Types.h>
  34. #include "Concordia.h"
  35. #include "DrawTkl.h"
  36. #include "SizeTkl.h"
  37.  
  38.  
  39. #pragma segment Main
  40. /******************************************************************************\
  41. * DoSizeMsg - Calculate the horizontal and vertical size of menu in pixels
  42. *
  43. * DoSizeMsg calculates the horizontal and vertical size in pixels of the menu
  44. * specified specified by TheMenu.  The menuWidth and menuHeight fields are
  45. * filled with this information.
  46. \******************************************************************************/
  47.  
  48. void
  49. DoSizeMsg (TheMenu)
  50.     MenuHandle TheMenu; //=> Menu to size <>
  51.     {
  52.     Str255      *ItemString; //-> Item's string
  53.     ItemInfoPtr ItemInfo;    //-> Item info
  54.     short       TestWidth;   //Width of item to test
  55.     short       Height;      //Height of menu
  56.     short       Width;       //Width of menu
  57.     short       ScrnHeight;  //Screen height in pixels w/o menu bar & margin
  58.  
  59.     Height = Width = 0;
  60.     ScrnHeight = qd.screenBits.bounds.bottom - qd.screenBits.
  61.             bounds.top - GetMBarHeight () - scrnMargin;
  62.     HLock ((Handle) TheMenu);
  63.     ItemString = (Str255 *) (**TheMenu).menuData;
  64.     ItemString = (Str255 *) ((Byte *) ItemString + strSize (*ItemString));
  65.     while ((*ItemString) [0] != (char) 0)
  66.         {
  67.         ItemInfo = (ItemInfoPtr) ((Byte *) *ItemString + strSize (*ItemString));
  68.         Height += CalcItemHeight (*ItemString, ItemInfo);
  69.         TestWidth = CalcItemWidth (*ItemString, ItemInfo);
  70.         if (TestWidth > Width)
  71.             Width = TestWidth;
  72.         ItemString = (Str255 *) (ItemInfo + 1);
  73.         }
  74.     HUnlock ((Handle) TheMenu);
  75.     (**TheMenu).menuWidth = Width;
  76.     if (Height > ScrnHeight)
  77.         (**TheMenu).menuHeight = ScrnHeight;
  78.     else
  79.         (**TheMenu).menuHeight = Height;
  80.     }
  81.  
  82.  
  83. #pragma segment Main
  84. /******************************************************************************\
  85. * CalcItemHeight - Calculate the height of a menu item in pixels
  86. *
  87. * CalcItemHeight calculates the height of the menu item (whose information is
  88. * specified by ItemData) in pixels.  The size of an icon and separation bars
  89. * are accounted for.  The height is returned.  ItemString is the item's string.
  90. * CurrFont is a FontInfo record containing information about the current item's
  91. * font specs, taking the current style into account.
  92. *
  93. * Coding Note:
  94. * #A# - If the first character of the item string is a hyphen, DrawItem (source
  95. *       in DrawTkl.c) will just draw a gray, horizontal line.  The height of any
  96. *       "gray, horizontal line" items will always be stdItemHeight.
  97. * #B# - These command-key equivalents indicate that the item's icon should be
  98. *       scaled to half its size.
  99. \******************************************************************************/
  100.  
  101. short
  102. CalcItemHeight (ItemString, ItemData)
  103.     Str255      ItemString; //Name of menu item >>
  104.     ItemInfoPtr ItemData;   //Information for menu item >>
  105.     {
  106.     short        TotHeight; //Total height of menu item in pixels
  107.     short        Height;    //Height of menu item in pixels
  108.     short        IconSize;  //Size of icon, including vertical margins
  109.     FontInfo     CurrFont;  //Current font's characteristics
  110.     TextStateRec TextState; //Current port's text characteristics
  111.  
  112.     TotHeight = itemVertMarg << 1;
  113.     if (ItemString [1] == '-') //#A#
  114.         TotHeight += stdItemHeight;
  115.     else
  116.         {
  117.         GetTextState (&TextState);
  118.         TextFace (ItemData->charStyle);
  119.         GetFontInfo (&CurrFont);
  120.         Height = CurrFont.ascent + CurrFont.descent + CurrFont.leading;
  121.         if (ItemData->iconNum != (Byte) 0)
  122.             {
  123.             if (ItemData->kbdEquiv == (char) 0x1D || ItemData->kbdEquiv ==
  124.                     (char) 0x1F) //#B#
  125.                 IconSize = (short) (iconSize / 2 + iconVertMarg * 2);
  126.             else
  127.                 IconSize = (short) (iconSize + iconVertMarg * 2);
  128.             if (IconSize > Height)
  129.                 Height = IconSize;
  130.             }
  131.         if (ItemData->kbdEquiv == (char) hMenuCmd)
  132.             if (sicnSize > Height)
  133.                 Height = sicnSize;
  134.         TotHeight += Height;
  135.         SetTextState (&TextState);
  136.         }
  137.     return TotHeight;
  138.     }
  139.  
  140.  
  141. #pragma segment Main
  142. /******************************************************************************\
  143. * CalcItemWidth - Calculate the width of a menu item in pixels
  144. *
  145. * CalcItemWidth calculates the width of the menu item whose item data is
  146. * specified by ItemData.  The item's string is given in ItemString, the specs
  147. * for the current font (with item styles) is given in CurrFont, and the
  148. * maximum width in pixels of the command-key equivalents is given in CmmdWidth.
  149. * The textFace field of the current GrafPort might be changed.
  150. *
  151. * Coding Notes
  152. * #A# - These command-key equivalents indicate that the item's icon should be
  153. *       scaled to half its size.
  154. \******************************************************************************/
  155.  
  156. short
  157. CalcItemWidth (ItemString, ItemData)
  158.     Str255      ItemString; //Name of menu item >>
  159.     ItemInfoPtr ItemData;   //Information for menu item >>
  160.     {
  161.     short        TotWidth;  //Total width of menu item in pixels (except mark)
  162.     short        Width;     //Height of menu item in pixels
  163.     FontInfo     CurrFont;  //Current font's characteristics
  164.     TextStateRec TextState; //Current port's text characteristics
  165.  
  166.     Width = 0;
  167.     GetFontInfo (&CurrFont);
  168.     TotWidth = (short) (itemHorzMarg << 1) + CurrFont.widMax + (short)
  169.             markItemGap;
  170.     if (ItemString [1] != '-')
  171.         {
  172.         GetTextState (&TextState);
  173.         if (ItemData->kbdEquiv > '!')
  174.             Width = CharWidth (cmmdCharCode) + CurrFont.widMax + cmmdItemGap;
  175.         TextFace (ItemData->charStyle);
  176.         Width += StringWidth (ItemString);
  177.         if (ItemData->iconNum != (Byte) 0)
  178.             if (ItemData->kbdEquiv == (char) 0x1D || ItemData->kbdEquiv ==
  179.                     (char) 0x1F) //#A#
  180.                 Width += (short) (iconSize / 2 + iconItemGap);
  181.             else
  182.                 Width += (short) (iconSize + iconItemGap);
  183.         if (ItemData->kbdEquiv == (char) hMenuCmd)
  184.             Width += sicnSize;
  185.         TotWidth += Width;
  186.         SetTextState (&TextState);
  187.         }
  188.     return TotWidth;
  189.     }
  190.